﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.SiteDefinition.Core;
using Kiiro.Model;

using AppName = Kiiro.Common.Utility.Constants.AppName;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class ErrorsList : System.Web.UI.UserControl
    {
        private BasePage basePage;
        public BasePage Base
        {
            get { return this.basePage; }
        }

        private List<string> errorMessages = new List<string> ( );
        public List<string> ErrorMessages
        {
            get { return errorMessages; }
            set { errorMessages = value; }
        }

        public OperationStatus OperationStatus
        {
            get;
            set;
        }

        protected override void OnInit ( EventArgs e )
        {
            this.basePage = this.Page is BasePage ? ( BasePage ) this.Page : new BasePage ( ).DefaultBasePage;
            base.OnInit ( e );
        }

        protected override void OnPreRender ( EventArgs e )
        {
            base.OnPreRender ( e );

            GenerateLicenseKeyMessages ( );

            if ( ErrorMessages.Count > 0 )
            {
                errorPanel.Visible = true;

                pnlErrorsList.Controls.Clear ( );
                foreach ( string errorMessage in ErrorMessages )
                {
                    pnlErrorsList.Controls.Add ( new LiteralControl ( errorMessage + "<br/>" ) );
                }

                if ( this.basePage.LicenseKeyBits.Contains ( true ) )
                    this.OperationStatus = OperationStatus.Failure;

                pnlErrorsList.CssClass = string.Format ( "wrap-inner notice-{0}", OperationStatus.ToString ( ).ToLower ( ) );
            }
        }

        private void GenerateLicenseKeyMessages ( )
        {
            LicensingManifest allKeysManifest = LicensingModel.GetAllKeysManifest ( null, Base.SiteCollectionId, Base.ServerRelativeUrl );
            int openProjectsCount = LicensingModel.GetOpenProjectsCount ( Base.SiteCollectionId, Base.ServerRelativeUrl );
            int activeUsersCount = LicensingModel.GetActiveUsersCount ( Base.SiteCollectionId, Base.ServerRelativeUrl );

            if ( Base.LicenseKeyBits [ 0 ] )
            {
                ErrorMessages.Add ( "Your License Key(s) have expired" );
            }

            string projectsMessage = "";
            if ( Base.PermissionBitsProjects [ 2 ] )
            {
                if ( Base.LicenseKeyBits [ 1 ] )
                {
                    projectsMessage = "projects has exceeded";
                }
                else if ( openProjectsCount == allKeysManifest.OpenProjectsLimit )
                {
                    projectsMessage = "projects is at";
                }
                else if ( openProjectsCount / allKeysManifest.OpenProjectsLimit > 0.8 && openProjectsCount / allKeysManifest.OpenProjectsLimit < 1 )
                {
                    projectsMessage = "projects is close to";
                }
            }

            string usersMessage = "";
            if ( Base.PermissionBitsUsers [ 2 ] )
            {
                if ( Base.LicenseKeyBits [ 2 ] )
                {
                    usersMessage = "users has exceeded";
                }
                else if ( activeUsersCount == allKeysManifest.ActiveUsersLimit )
                {
                    usersMessage = "users is at";
                }
                else if ( activeUsersCount / allKeysManifest.ActiveUsersLimit > 0.8 && activeUsersCount / allKeysManifest.ActiveUsersLimit < 1 )
                {
                    usersMessage = "users is close to";
                }
            }

            if ( !string.IsNullOrEmpty ( projectsMessage ) || !string.IsNullOrEmpty ( usersMessage ) )
            {
                string and = !string.IsNullOrEmpty ( projectsMessage ) && !string.IsNullOrEmpty ( usersMessage ) ? " and " : "";
                string projects = !string.IsNullOrEmpty ( projectsMessage ) ? "projects" : "";
                string users = !string.IsNullOrEmpty ( usersMessage ) ? "users" : "";

                string templateMessage = "The number of {0}{1}{2} the maximum allowed within your license. Upgrade now to ensure that you have enough {3}{1}{4} to continue working effectively";

                ErrorMessages.Add ( string.Format ( templateMessage, projectsMessage, and, usersMessage, projects, users ) );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/ErrorsList.ascx.cs $
 * 
 * 10    1/06/10 12:39p Camerons
 * 
 * 9     4/01/10 12:03p Adamw
 * 
 * 8     16/12/09 2:20p Adamw
 * 
 * 7     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/